/**
 *
 */
package com.angel.common.providers.managers;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import com.angel.annotation.managers.AnnotationManager;
import com.angel.common.helpers.AnnotationHelper;
import com.angel.common.helpers.ReflectionHelper;
import com.angel.common.providers.annotations.RangeValue;
import com.angel.common.providers.enums.RangeValueType;


/**
 * @author William
 *
 */
public class FieldRangeValueAnnotationManager extends AnnotationManager{

	private static final String ACTION = "_ACTION";

	public static final String RANGE_VALUE_MIDDLE_ACTION = RangeValueType.MIDDLE.getValue() + ACTION;
	public static final String RANGE_VALUE_MINIMUN_ACTION = RangeValueType.MINIMUN.getValue() + ACTION;
	public static final String RANGE_VALUE_MAXIMUN_ACTION = RangeValueType.MAXIMUN.getValue() + ACTION;


	public static final Class<RangeValue> FIELD_RANGE_VALUE_ANNOTATION = RangeValue.class;

	{
		/*
		super.addAction(RANGE_VALUE_MINIMUN_ACTION, new MinimumFieldRangeValueAnnotationAction());
		super.addAction(RANGE_VALUE_MIDDLE_ACTION, new MiddleFieldRangeValueAnnotationAction());
		super.addAction(RANGE_VALUE_MAXIMUN_ACTION, new MaximumFieldRangeValueAnnotationAction());
		*/

	}

	public FieldRangeValueAnnotationManager(){
		super();
	}

	private boolean canFindRangeValueFor(RangeValueType rangeValueType, Class<?> objectProviderClass) {
		/*String actionName = this.findActionNameFor(rangeValueType);
		AnnotationAction annotationAction = super.findAnnotationAction(actionName);
		return annotationAction.canDoActionFor(this, objectProviderClass);*/
		return false;
	}

	public boolean canFindAllRangeValueFor(Class<?> objectProvider){
		return	this.canFindRangeValueFor(RangeValueType.MINIMUN, objectProvider) &&
				this.canFindRangeValueFor(RangeValueType.MIDDLE, objectProvider) &&
				this.canFindRangeValueFor(RangeValueType.MAXIMUN, objectProvider);
	}

	public boolean canFindRangeValueFor(Class<?> objectProvider){
		return	this.canFindRangeValueFor(RangeValueType.MINIMUN, objectProvider) &&
				this.canFindRangeValueFor(RangeValueType.MAXIMUN, objectProvider);
	}

	public Comparable<?> findRangeValueFor(RangeValueType rangeValueType, Object object) {
		/*
		String actionName = this.findActionNameFor(rangeValueType);
		AnnotationAction annotationAction = super.findAnnotationAction(actionName);
		return (Comparable<?>) annotationAction.doAction(this, object);
		*/
		return null;
	}

	public Boolean canProcessAnnotationAt(Object object) {
		return	AnnotationHelper.hasAnnotation(ReflectionHelper.getFieldsDeclaredFor(object.getClass()), this.getClassAnnotation()) &&
				AnnotationHelper.hasOnlyOneAnnotationInstance(ReflectionHelper.getFieldsDeclaredFor(object.getClass()), this.getClassAnnotation()) &&
				this.canFindRangeValueFor(object.getClass());
	}

	protected Annotation getAnnotationImpl(Object object) {
		Field field = ReflectionHelper.getAllFieldWith(this.getClassAnnotation(), object.getClass())[0];
		return field.getAnnotation(this.getClassAnnotation());
	}

	public Class<? extends Annotation> getClassAnnotation() {
		return RangeValue.class;
	}

	@SuppressWarnings("unused")
	private String findActionNameFor(RangeValueType rangeValueType){
		return (rangeValueType != null) ? rangeValueType.getValue() + ACTION: null;
	}

}
